package com.rainflowerstone.home.components;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmuidemo.base.BaseFragment;
import com.qmuiteam.qmuidemo.lib.Group;
import com.qmuiteam.qmuidemo.lib.annotation.Widget;
import com.qmuiteam.qmuidemo.manager.QDDataManager;
import com.qmuiteam.qmuidemo.model.QDItemDescription;
import com.rainflowerstone.R;
import com.rainflowerstone.adapter.HomeScrollableTabAdapter;
import com.rainflowerstone.business.components.AssortmentFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ Author     ：lwc
 * @ Date       ：Created in 17:01 2018/12/27
 * @ Description：${description}
 * @ Modified By：
 * @Version: $version$
 */
@Widget(group = Group.Other, name = "测试首页")
public class HomeScrollableTabFragment extends BaseFragment {

    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;
    @BindView(R.id.tabSegment)
    QMUITabSegment mTabSegment;
    @BindView(R.id.contentViewPager)
    ViewPager mContentViewPager;
    private QDItemDescription mQDItemDescription;

    private HomeScrollableTabAdapter mHomeScrollableTabAdapter;
    @Override
    protected View onCreateView() {
        View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.rain_flower_stone_fragment_homescrollabletab, null);
        // 沉浸式状态栏
        QMUIStatusBarHelper.translucent(getActivity());
        ButterKnife.bind(this, rootView);
        mQDItemDescription = QDDataManager.getInstance().getDescription(this.getClass());
        initTopBar();
        initTabAndPager();
        return rootView;
    }

    private void initTopBar(){
//        mTopBar.setTitle(mQDItemDescription.getName());
//        mTopBar.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.app_color_theme_10));
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.rain_flower_stone_fragment_home_topbar, null);
        //为中间标题设置自己的view
        mTopBar.setCenterView(view);
    }

    private void initTabAndPager(){
        mHomeScrollableTabAdapter = new HomeScrollableTabAdapter(getActivity().getSupportFragmentManager());
        mContentViewPager.setOffscreenPageLimit(9);
        mContentViewPager.setAdapter(mHomeScrollableTabAdapter);
        QMUITabSegment.Tab TAB01 = new QMUITabSegment.Tab("推荐");
        QMUITabSegment.Tab TAB02 = new QMUITabSegment.Tab("列一");
        QMUITabSegment.Tab TAB03 = new QMUITabSegment.Tab("列二");
        QMUITabSegment.Tab TAB04 = new QMUITabSegment.Tab("列三");
        QMUITabSegment.Tab TAB05 = new QMUITabSegment.Tab("列四");
        QMUITabSegment.Tab TAB06 = new QMUITabSegment.Tab("列五");
        QMUITabSegment.Tab TAB07 = new QMUITabSegment.Tab("列六");
        QMUITabSegment.Tab TAB08 = new QMUITabSegment.Tab("列七");
        QMUITabSegment.Tab TAB09 = new QMUITabSegment.Tab("列八");
        QMUITabSegment.Tab TAB10 = new QMUITabSegment.Tab("其他");
        mTabSegment.addTab(TAB01);
        mTabSegment.addTab(TAB02);
        mTabSegment.addTab(TAB03);
        mTabSegment.addTab(TAB04);
        mTabSegment.addTab(TAB05);
        mTabSegment.addTab(TAB06);
        mTabSegment.addTab(TAB07);
        mTabSegment.addTab(TAB08);
        mTabSegment.addTab(TAB09);
        mTabSegment.addTab(TAB10);
        QMUITabSegment.Tab tab = mTabSegment.getTab(0);
        //打开小红点
        tab.setSignCountMargin(0, -QMUIDisplayHelper.dp2px(getContext(), 2));
        tab.showSignCountView(getContext(), 0);
        int space = QMUIDisplayHelper.dp2px(getContext(), 25);//item之间的宽度
        int paddingLeftAndRight = QMUIDisplayHelper.dp2px(getContext(), 16);//距离左右边的距离
        mTabSegment.setHasIndicator(true);
        mTabSegment.setMode(QMUITabSegment.MODE_SCROLLABLE);
        mTabSegment.setItemSpaceInScrollMode(space);
        mTabSegment.setupWithViewPager(mContentViewPager, false);
        mTabSegment.setPadding(paddingLeftAndRight, 0, paddingLeftAndRight, 0);
        mTabSegment.addOnTabSelectedListener(new QMUITabSegment.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int index) {
                //隐藏小红点
                Toast.makeText(getContext(), "select index " + index, Toast.LENGTH_SHORT).show();
                mTabSegment.hideSignCountView(index);
            }

            @Override
            public void onTabUnselected(int index) {
                Toast.makeText(getContext(), "unSelect index " + index, Toast.LENGTH_SHORT).show();
                QMUITabSegment.Tab tab = mTabSegment.getTab(index);
                //打开小红点
                tab.setSignCountMargin(0, -QMUIDisplayHelper.dp2px(getContext(), 2));
                tab.showSignCountView(getContext(), 0);
            }

            @Override
            public void onTabReselected(int index) {
                Toast.makeText(getContext(), "reSelect index " + index, Toast.LENGTH_SHORT).show();
                QMUITabSegment.Tab tab = mTabSegment.getTab(index);
                //打开小红点
                tab.setSignCountMargin(0, -QMUIDisplayHelper.dp2px(getContext(), 2));
                tab.showSignCountView(getContext(), 0);
            }

            @Override
            public void onDoubleTap(int index) {
                Toast.makeText(getContext(), "double tap index " + index, Toast.LENGTH_SHORT).show();
            }
        });
    }

    //展示全部TAB项
    @OnClick(R.id.iv_listSearch)
    void search() {
        AssortmentFragment fragment = new AssortmentFragment();
        startFragment(fragment);
    }

}
